<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Memcached," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="1.memcached安装1[root@memcached ~]# yum install -y epel-release 1[root@memcached ~]# yum install -y libevent memcached libmemcached &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;启动 /etc/init.d/memcach">
<meta name="keywords" content="Memcached">
<meta property="og:type" content="article">
<meta property="og:title" content="memcached安装">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/03/Memcached/2. memcached安装/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="1.memcached安装1[root@memcached ~]# yum install -y epel-release 1[root@memcached ~]# yum install -y libevent memcached libmemcached &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;启动 /etc/init.d/memcach">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/01.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/02.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/03.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/04.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/05.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/06.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/07.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/08.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/09.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/10.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/11.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/12.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/13.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/14.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/15.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/16.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/17.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/18.png?raw=true">
<meta property="og:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/19.png?raw=true">
<meta property="og:updated_time" content="2017-09-01T08:55:05.010Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="memcached安装">
<meta name="twitter:description" content="1.memcached安装1[root@memcached ~]# yum install -y epel-release 1[root@memcached ~]# yum install -y libevent memcached libmemcached &amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;启动 /etc/init.d/memcach">
<meta name="twitter:image" content="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/01.png?raw=true">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/03/Memcached/2. memcached安装/"/>





  <title>memcached安装 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/03/Memcached/2. memcached安装/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">memcached安装</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-03T02:04:45+08:00">
                2017-09-03
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="1-memcached安装"><a href="#1-memcached安装" class="headerlink" title="1.memcached安装"></a>1.memcached安装</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># yum install -y epel-release</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># yum install -y libevent memcached libmemcached</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动 /etc/init.d/memcached start</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># service memcached start</span></div><div class="line">正在启动 memcached： [确定]</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># ps aux |grep memcached</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/01.png?raw=true" alt="01"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;memcached -d -p 11211 -u memcached -m 64 -c 1024 -P /var/run/memcached/memcached.pid</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;相关的参数在/etc/init.d/memcached和/etc/sysconfig/memcached中定义</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># vim /etc/sysconfig/memcached</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/02.png?raw=true" alt="02"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/03.png?raw=true" alt="03"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将内存改为 128m</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># /etc/init.d/memcached restart</span></div><div class="line">停止 memcached：     [确定]</div><div class="line">正在启动 memcached：  [确定]</div></pre></td></tr></table></figure>
<h3 id="memcached-启动参数"><a href="#memcached-启动参数" class="headerlink" title="memcached 启动参数"></a>memcached 启动参数</h3><ul>
<li>-d选项是启动一个守护进程</li>
<li>-m是分配给Memcache使用的内存数量，单位是MB，这里是200MB </li>
<li>-u是运行Memcache的用户，如果当前为 root 的话，需要使用此参数指定用户。 </li>
<li>-l是监听的服务器IP地址</li>
<li>-p是设置Memcache监听的端口，默认是11211</li>
<li>-c选项是最大运行的并发连接数，默认是1024</li>
<li>-P是设置保存Memcache的pid文件 </li>
</ul>
<h3 id="查看-memcached-运行状态"><a href="#查看-memcached-运行状态" class="headerlink" title="查看 memcached 运行状态"></a>查看 memcached 运行状态</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># memcached-tool 127.0.0.1:11211 stats</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/04.png?raw=true" alt="04"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># echo stats | nc 127.0.0.1:11211</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;需要安装nc工具</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># yum install -y nc</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/05.png?raw=true" alt="05"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;若安装libmemcached后，可以使用命令 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;memstat –servers=127.0.0.1:11211 查看memcached服务状态</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># memstat --servers=127.0.0.1:11211</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/06.png?raw=true" alt="06"></p>
<h2 id="2-memcached-基本命令"><a href="#2-memcached-基本命令" class="headerlink" title="2.memcached 基本命令"></a>2.memcached 基本命令</h2><h3 id="memcached-命令行"><a href="#memcached-命令行" class="headerlink" title="memcached 命令行"></a>memcached 命令行</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># telnet 127.0.0.1 11211</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/07.png?raw=true" alt="07"></p>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/08.png?raw=true" alt="08"></p>
<h3 id="mamcached-语法规则"><a href="#mamcached-语法规则" class="headerlink" title="mamcached 语法规则"></a>mamcached 语法规则</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;<span class="built_in">command</span> name&gt; &lt;key&gt; &lt;flags&gt; &lt;exptime&gt; &lt;bytes&gt;\r\n &lt;data block&gt;\r\n</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：\r\n在windows下是Enter键</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;a) <command name=""> 可以是”set”, “add”, “replace”。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;“set”表示按照相应的<key>存储该数据，没有的时候增加，有的覆盖。</key></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; “add”表示按照相应的<key>添加该数据,但是如果该<key>已经存在则会操作失败。</key></key></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; “replace”表示按照相应的<key>替换数据,但是如果该<key>不存在则操作失败。</key></key></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;b) <key> 客户端需要保存数据的key。</key></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;c) <flags> 是一个16位的无符号的整数(以十进制的方式表示)。</flags></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该标志将和需要存储的数据一起存储,并在客户端get数据时返回。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户可以将此标志用做特殊用途，此标志对服务器来说是不透明的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;d) <exptime> 过期的时间。</exptime></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;若为0表示存储的数据永远不过期(但可被服务器算法：LRU 等替换)。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果非0(unix时间或者距离此时的秒数),当过期后,服务器可以保证用户得不到该数据(以服务器时间为标准)。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;e) <bytes> 需要存储的字节数，当用户希望存储空数据时<bytes>可以为0</bytes></bytes></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;f) <data block="">需要存储的内容，输入完成后，最后客户端需要加上”\r\n”（直接点击Enter）作为”命令头”的结束标志。</data></p>
<h3 id="memcached-数据示例"><a href="#memcached-数据示例" class="headerlink" title="memcached 数据示例"></a>memcached 数据示例</h3><p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/09.png?raw=true" alt="09"></p>
<h2 id="3-PHP-连接-memcached"><a href="#3-PHP-连接-memcached" class="headerlink" title="3.PHP 连接 memcached"></a>3.PHP 连接 memcached</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;先安装php的memcache扩展</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># wget http://www.lishiming.net/data/attachment/forum/memcache-2.2.3.tgz</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># tar zxf memcache-2.2.3.tgz </span></div><div class="line">[root@memcached ~]<span class="comment"># cd memcache-2.2.3</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached memcache-2.2.3]<span class="comment"># /usr/local/php/bin/phpize</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/10.png?raw=true" alt="10"></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached memcache-2.2.3]<span class="comment"># ./configure --with-php-config=/usr/local/php/bin/php-config</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached memcache-2.2.3]<span class="comment"># make &amp;&amp; make install</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装完后会有类似这样的提示：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">Installing shared extensions: /usr/<span class="built_in">local</span>/php/lib/php/extensions/no-debug-non-zts-20131226/</div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/11.png?raw=true" alt="11"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把这个记住，然后修改php.ini</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached memcache-2.2.3]<span class="comment"># vim /usr/local/php/etc/php.ini</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把 extension_dir = “./“ 修改为<br>extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/“</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/12.png?raw=true" alt="12"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/13.png?raw=true" alt="13"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;并添加一行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">extension = <span class="string">"memcache.so"</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/14.png?raw=true" alt="14"></p>
<h3 id="PHP-连接-memcached-检验"><a href="#PHP-连接-memcached-检验" class="headerlink" title="PHP 连接 memcached 检验"></a>PHP 连接 memcached 检验</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;检查模块是否添加正常</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached memcache-2.2.3]<span class="comment"># /usr/local/php/bin/php -m | grep memcache</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/15.png?raw=true" alt="15"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;模块中有 memcache 说明配置成功</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载测试php脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached memcache-2.2.3]<span class="comment"># curl www.apelearn.com/study_v2/.memcache.txt &gt; 1.php 2&gt;/dev/null</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;执行脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached memcache-2.2.3]<span class="comment"># /usr/local/php/bin/php 1.php</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/16.png?raw=true" alt="16"></p>
<h2 id="4-memcached-实现-session-共享"><a href="#4-memcached-实现-session-共享" class="headerlink" title="4.memcached 实现 session 共享"></a>4.memcached 实现 session 共享</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本实例是在lamp/lnmp环境下实现</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑php.ini添加两行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># vim /usr/local/php/etc/php.ini</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">session.save_handler = memcache </div><div class="line">session.save_path = <span class="string">"tcp://127.0.0.1:11211"</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/17.png?raw=true" alt="17"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果 session 服务器在远程这里输入远程的 IP 地址</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者httpd.conf中对应的虚拟主机中添加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># vim /usr/local/apache2/conf/httpd.conf</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">php_value session.save_handler <span class="string">"memcache"</span> </div><div class="line">php_value session.save_path <span class="string">"tcp://192.168.0.9:11211"</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 或者php-fpm.conf对应的pool中添加</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">php_value[session.save_handler] = memcache</div><div class="line">php_value[session.save_path] = <span class="string">" tcp://192.168.0.9:11211 "</span></div></pre></td></tr></table></figure>
<h3 id="memcached-session测试"><a href="#memcached-session测试" class="headerlink" title="memcached session测试"></a>memcached session测试</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># wget http://study.lishiming.net/.mem_se.txt</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># mv .mem_se.txt /usr/local/apache2/htdocs/session.php</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># curl localhost/session.php</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/18.png?raw=true" alt="18"></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;类似于1443702394<br><br>1443702394<br><br>i44nunao0g3o7vf2su0hnc5440</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@memcached ~]<span class="comment"># telnet 127.0.0.1 11211</span></div></pre></td></tr></table></figure>
<p><img src="https://github.com/hcldirgit/image/blob/master/memcached%E5%AE%89%E8%A3%85/19.png?raw=true" alt="19"></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Memcached/" rel="tag"># Memcached</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/03/Git/2.  github常见操作和常见错误  错误提示  fatal   remote origin already exists./" rel="next" title="github常见操作和常见错误  错误提示  fatal   remote origin already exists.">
                <i class="fa fa-chevron-left"></i> github常见操作和常见错误  错误提示  fatal   remote origin already exists.
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/03/Memcached/1. Memcached/" rel="prev" title="Memcached">
                Memcached <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-memcached安装"><span class="nav-number">1.</span> <span class="nav-text">1.memcached安装</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#memcached-启动参数"><span class="nav-number">1.1.</span> <span class="nav-text">memcached 启动参数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看-memcached-运行状态"><span class="nav-number">1.2.</span> <span class="nav-text">查看 memcached 运行状态</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-memcached-基本命令"><span class="nav-number">2.</span> <span class="nav-text">2.memcached 基本命令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#memcached-命令行"><span class="nav-number">2.1.</span> <span class="nav-text">memcached 命令行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#mamcached-语法规则"><span class="nav-number">2.2.</span> <span class="nav-text">mamcached 语法规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#memcached-数据示例"><span class="nav-number">2.3.</span> <span class="nav-text">memcached 数据示例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-PHP-连接-memcached"><span class="nav-number">3.</span> <span class="nav-text">3.PHP 连接 memcached</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-连接-memcached-检验"><span class="nav-number">3.1.</span> <span class="nav-text">PHP 连接 memcached 检验</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-memcached-实现-session-共享"><span class="nav-number">4.</span> <span class="nav-text">4.memcached 实现 session 共享</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#memcached-session测试"><span class="nav-number">4.1.</span> <span class="nav-text">memcached session测试</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
